﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.Utils.JObject
 * 接口名称 SimpleJObject
 * 开发人员：zdm
 * 创建时间：2023/12/12 13:51:12
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools
{
	public class SimpleJObject: ISimpleJobject
	{
		public SimpleJObject() { }

		public T  GetValue<T>(JObject jObject,params string [] names)
		{
			var token=GetToken(jObject,names);
			return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(token));
		}

		public object GetValue(JObject jObject,Type type, params string[] names)
		{
			var token = GetToken(jObject, names);
			return JsonConvert.DeserializeObject(JsonConvert.SerializeObject(token),type);
		}
		private JToken GetToken(JObject token,params string[] names)
		{
			if (names == null || token == null)
			{
				return null;
			}
			JToken targetToken = token;
			int i = 0;
			foreach (var name in names)
			{
			
				if (!string.IsNullOrEmpty(name) && token[name] != null)
				{
					if(i==0){
						targetToken = token[name];
					}
					else
					{
						targetToken=targetToken[name];
					}
					i++;
				}
				else
				{
					break;
				}
			}
			return targetToken;
		}
	}
}
